package cn.sdack.go.common.entities.users;

import cn.sdack.go.common.entities.BaseEntity;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import org.hibernate.validator.constraints.Length;

import java.util.ArrayList;
import java.util.List;

/**
 * @author sdack
 * @date 2024/2/22
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "region",indexes = {
        @Index(name = "idx_region_codeAndPcode", columnList = "code,pcode"),
        @Index(name = "idx_region_pid", columnList = "pid")
})
public class RegionEntity extends BaseEntity {

    @NotNull(message = "ID - 必填",groups = {BaseEntity.Set.class})
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NonNull
    @NotNull(message = "上级ID - 必填",groups = {BaseEntity.Add.class,BaseEntity.Set.class})
    @Column(nullable = false)
    private Long pid;

    @NonNull
    @NotNull(message = "上级区划代码 - 必填",groups = {BaseEntity.Add.class,BaseEntity.Set.class})
    @Column(nullable = false)
    private Long pcode;

    @NonNull
    @NotNull(message = "区划代码 - 必填",groups = {BaseEntity.Add.class,BaseEntity.Set.class})
    @Column(nullable = false)
    private Long code;

    @NonNull
    @NotNull(message = "名称 - 必填",groups = {BaseEntity.Add.class,BaseEntity.Set.class})
    @Length(max = 30,message = "名称 - 长度不正确",groups = {BaseEntity.Add.class,BaseEntity.Set.class})
    @Column(length = 30, nullable = false)
    private String name;

    @Builder.Default
    @Column(nullable = false)
    private Double lon = 0.0;

    @Builder.Default
    @Column(nullable = false)
    private Double lat = 0.0;

    @Builder.Default
    @Column(nullable = false)
    private Integer level = 0;

    @Builder.Default
    @Column(nullable = false)
    private Integer sort = 0;

    @Builder.Default
    @Column(length = 50,nullable = false)
    private String remark = "";

    @Builder.Default
    @Transient
    private List<RegionEntity> child = new ArrayList<>();

}
